package com.ruoyi.equipment.service.impl;

import com.ruoyi.common.annotation.DataScope;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.equipment.domain.TDrawer;
import com.ruoyi.equipment.domain.TEquipment;
import com.ruoyi.equipment.mapper.TDrawerMapper;
import com.ruoyi.equipment.mapper.TEquipmentMapper;
import com.ruoyi.equipment.service.ITEquipmentService;
import com.ruoyi.equipment.util.EmqxUtils;
import com.ruoyi.web.controller.monitor.ServerController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 智能设备表Service业务层处理
 *
 * @author ruoyi
 * @date 2021-12-15
 */
@Service
public class TEquipmentServiceImpl implements ITEquipmentService {
    @Autowired
    private TEquipmentMapper tEquipmentMapper;

    @Autowired
    private TDrawerMapper tDrawerMapper;

    /**
     * 查询智能设备表
     *
     * @param equipmentId 智能设备表主键
     * @return 智能设备表
     */
    @Override
    public TEquipment selectTEquipmentByEquipmentId(String equipmentId) {
        return tEquipmentMapper.selectTEquipmentByEquipmentId(equipmentId);
    }

    /**
     * 查询智能设备表列表
     *
     * @param tEquipment 智能设备表
     * @return 智能设备表
     */
    @Override
    @DataScope(deptAlias = "d")
    public List<TEquipment> selectTEquipmentList(TEquipment tEquipment) {
        return tEquipmentMapper.selectTEquipmentList(tEquipment);
    }

    /**
     * 新增部署智能设备表
     *
     * @param tEquipment 智能设备表
     * @return 结果
     */
    @Override
    @Transactional
    public int insertTEquipment(TEquipment tEquipment) {
        //新增设备表时同时增加仓匣数目

        return tEquipmentMapper.updateTEquipment(tEquipment);
    }

    /**
     * 修改智能设备表
     *
     * @param tEquipment 智能设备表
     * @return 结果
     */
    @Override
    public int updateTEquipment(TEquipment tEquipment) {
        tEquipment.setUpdateTime(DateUtils.getNowDate());
        return tEquipmentMapper.updateTEquipment(tEquipment);
    }

    /**
     * 批量下线智能设备表
     *
     * @param equipmentIds 需要删除的智能设备表主键
     * @return 结果
     */
    @Override
    public int deleteTEquipmentByEquipmentIds(String[] equipmentIds) {

        //下线设备
        for (int i = 0; i < equipmentIds.length; i++) {
            String equipmentId = equipmentIds[i];
            EmqxUtils.deleteClient("http://8.141.144.103:8081/api/v4/clients/" + equipmentId);
        }
        //修改表中的在线状态
        return tEquipmentMapper.deleteTEquipmentByEquipmentIds(equipmentIds);
    }

    /**
     * 删除智能设备表信息
     *
     * @param equipmentId 智能设备表主键
     * @return 结果
     */
    @Override
    public int deleteTEquipmentByEquipmentId(Integer equipmentId) {

        return tEquipmentMapper.deleteTEquipmentByEquipmentId(equipmentId);
    }

    //检查设备是否存在
    @Override
    public int checkEquipment(String equipmentId) {
        int num = tEquipmentMapper.selectOne(equipmentId);
        return num;

    }

    //上线设备
    @Override
    public int upTEquipmentByEquipmentIds(String[] equipmentIds) {
        int num = tEquipmentMapper.upTEquipmentByEquipmentIds(equipmentIds);
        return num;
    }
}
